/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file nodeIconManager.h

  \author Cory Dixon
*/

#ifndef __FB_LAYER_MANAGER_H
#define __FB_LAYER_MANAGER_H

#include "LayerManager.h"

#ifdef USING_KML
 #include "kml/dom.h"
#endif

#define MAX_BOUNDARIES 128

//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
typedef struct _ImgPoints_t
{
	double x;
	double y;
} ImgPoints_t;

typedef struct _BoundaryPoint_t
{
	float lat;
	float lon;
	float min_alt;
	float max_alt;

	/* Initialize waypoint */
	_BoundaryPoint_t() {
		clear();
	}

	void clear() {
		lat     = NAN;
		lon     = NAN;
		min_alt = INFINITY;
		max_alt = -INFINITY;
	};
	
	// friend functions
	friend ostream& operator << (ostream& ostrm, _BoundaryPoint_t& p){
		ostrm 
			<< setprecision (9) 
			<< p.lat << " "
			<< p.lon << " "
			<< p.min_alt << " "
			<< p.max_alt << " ";

		// reset output format
		return ostrm;
   }
 
	friend istream& operator >> (istream& istrm, _BoundaryPoint_t& point)
	{
		istrm 
			>> point.lat
			>> point.lon
			>> point.min_alt
			>> point.max_alt;

		return istrm;
	}
} BoundaryPoint_t;


typedef struct _FlightBoundaryAlloc 
{
	gfloat          scale;
	gboolean        draw;

	GooCanvasItem  *icon;
	GooCanvasItem  *text;
	char            text_str[128];
	char            color_str[32];

	int             numPoints;
	BoundaryPoint_t b_point[32];
	String          name;
	ImgPoints_t     imgPoints[32];
	GooCanvasItem  *item;

	_FlightBoundaryAlloc() { icon = text = item = NULL; scale = 1.0; }
	~_FlightBoundaryAlloc() { 
		if( item ) goo_canvas_item_remove( item ); item = NULL;
		icon = NULL;
		text = NULL;
	}

} FlightBoundaryAlloc;


class FBManager : public LayerManager 
#ifdef USING_KML
  , public kmldom::ParserObserver
#endif
{
	protected:
		FlightBoundaryAlloc fb[MAX_BOUNDARIES+1];

		double lineWidth;
		int num_boundaries;

		// menu  widget
		GtkWidget      *menu;
		GtkActionGroup *action_group;          /* Packing group for our Actions */
		GtkUIManager   *menu_manager;          /* The magic widget! */
#ifdef USING_KML
		bool AddChild(const kmldom::ElementPtr& parent, const kmldom::ElementPtr& child);
#endif

	public:
		FBManager();
		~FBManager();

		virtual void init(GPSMap *map = NULL);
		virtual void updateMap();
		virtual void translate_from_zoomChange(double zoom_factor);

		// functions for the flight boundary layer
		void setup_item(GooCanvasItem *item);
		void create_canvas_flightboundary(int boundaryInd, uint32_t fill_color = 0x0000FF22);
		void create_canvas_line_flightboundary(int boundaryInd);
		bool read_from_file(String file);
		int create_control_window( GdkEventButton *event, String name);
		void clearFB();

		static bool motion_notify_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, GdkEventMotion *event, FBManager *me);
		static bool button_press_event_cb (GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, FBManager *me);
		static bool button_release_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, FBManager *me);
		static int clear_fb_cb(GtkAction *action, FBManager *me);
};

#endif
